﻿using System.Net.Mail;
using System.Web.Mvc;
using RotaractProjectCalendar.Models;

namespace RotaractProjectCalendar.Areas.Anonymous.Controllers
{
    public class ContactController : Controller
    {
        //
        // GET: /SendMail/
        public ActionResult Index()
        {
            var mailDetails = new Mail { To = "tomasz.synak@gmail.com" };

            return View(mailDetails);
        }

        //
        // POST: /SendMail/
        [HttpPost]
        public ActionResult Index(Mail objModelMail)
        {
            try
            {
                if(ModelState.IsValid)
                {
                    var mail = new MailMessage();
                    mail.To.Add(objModelMail.To);
                    mail.From = new MailAddress(objModelMail.From);
                    mail.Subject = objModelMail.Subject;
                    var body = objModelMail.Body;
                    mail.Body = mail.From + "<br/><br/>" + body;
                    mail.IsBodyHtml = true;
                    var smtp = new SmtpClient
                    {
                        Host = "smtp.gmail.com",
                        Port = 587,
                        UseDefaultCredentials = false,
                        Credentials = new System.Net.NetworkCredential
                            ("rotaract.events.calendar@gmail.com", ""), /*password*/
                        EnableSsl = true
                    };
                    smtp.Send(mail);

                    return RedirectToAction("Index", "Home");
                }
            }

            catch
            {
                ModelState.AddModelError("", "Unable to send message. Try again, and if the problem persists see your system administrator.");
            }

            return View(objModelMail);
        }
    }
}